package com.wude.guild.fragment;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.mc.developmentkit.utils.ToastUtil;
import com.mc.developmentkit.views.SimpleFooter;
import com.mc.developmentkit.views.SimpleHeader;
import com.mc.developmentkit.views.SpringView;
import com.umeng.analytics.MobclickAgent;
import com.wude.bean.PinglunBean;
import com.wude.bean.UserInfo;
import com.wude.guild.R;
import com.wude.guild.adapter.MyPingLunAdapter;
import com.wude.guild.servercore.mints.TitleBaseFragment;
import com.wude.http.HttpCom;
import com.wude.http.HttpUtils;
import com.wude.tools.Utils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * Created by Administrator on 2017/11/23 0023.
 * <p>
 * 我的评论
 */
@SuppressLint("HandlerLeak")
public class MyCommentFragment extends TitleBaseFragment {
    private Unbinder unbinder;

    @BindView(R.id.pl_listview)
    ListView plListview;
    @BindView(R.id.pl_springview)
    SpringView plSpringview;
    @BindView(R.id.you)
    LinearLayout you;
    @BindView(R.id.error_text)
    TextView errorText;
    @BindView(R.id.error_layout)
    RelativeLayout errorLayout;
    @BindView(R.id.tiao)
    TextView tiao;
    private MyPingLunAdapter myPingLunAdapter;
    private UserInfo loginUser;
    private int limit = 1;
    List<PinglunBean> list = new ArrayList<>();

    @Override
    protected View createView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.my_comment_fragment, container, false);
    }

    @Override
    public void onResume() {
        super.onResume();
        MobclickAgent.onPageStart("MyCommentFragment");
    }

    @Override
    public void onPause() {
        super.onPause();
        MobclickAgent.onPageEnd("MyCommentFragment");
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        unbinder = ButterKnife.bind(this, view);

        setHeaderTitle("我的评论");

        myPingLunAdapter = new MyPingLunAdapter(getActivity());
        plListview.setAdapter(myPingLunAdapter);
        initdata();
    }

    private void initdata() {
        plSpringview.setType(SpringView.Type.FOLLOW);
        plSpringview.setListener(onFreshListener);
        plSpringview.setHeader(new SimpleHeader(getContext()));
        plSpringview.setFooter(new SimpleFooter(getContext()));
        initAndReflsh();
    }


    SpringView.OnFreshListener onFreshListener = new SpringView.OnFreshListener() {
        @Override
        public void onRefresh() {
            initAndReflsh();
        }

        @Override
        public void onLoadmore() {
            onLoadMord();
        }
    };

    private void onLoadMord() {
        limit = limit + 1;
        HashMap<String, String> map = new HashMap<>();
        map.put("token", loginUser.token);
        map.put("p", limit + "");
        HttpCom.POST(mhandler, HttpCom.getMyPinglunURL(), map, false);
    }

    private void initAndReflsh() {
        limit = 1;
        list.clear();
        loginUser = Utils.getLoginUser();
        HashMap<String, String> map = new HashMap<>();
        map.put("token", loginUser.token);
        map.put("p", limit + "");
        HttpCom.POST(handler, HttpCom.getMyPinglunURL(), map, false);
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    /**
     * 刚进来请求
     */
    Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            plSpringview.onFinishFreshAndLoad();
            switch (msg.what) {
                case 1:
                    List<PinglunBean> pinglunBeen = HttpUtils.DNSMyPinglun(msg.obj.toString(), tiao);
                    if (pinglunBeen != null) {
                        you.setVisibility(View.VISIBLE);
                        errorLayout.setVisibility(View.GONE);
                        list.addAll(pinglunBeen);
                        myPingLunAdapter.setList(list);
                    } else {
                        you.setVisibility(View.GONE);
                        errorLayout.setVisibility(View.VISIBLE);
                        errorText.setText("目前还没有发表过评论哦~");
                    }
                    break;
                case 2:

                    break;
            }
        }
    };
    /**
     * 加载更多请求
     */
    Handler mhandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            plSpringview.onFinishFreshAndLoad();
            switch (msg.what) {
                case 1:
                    List<PinglunBean> pinglunBeen = HttpUtils.DNSMyPinglun(msg.obj.toString(), tiao);
                    if (pinglunBeen != null) {
                        list.addAll(pinglunBeen);
                        myPingLunAdapter.setList(list);
                    } else {
                        ToastUtil.showToast("已经到底了~");
                    }
                    break;
                case 2:

                    break;
            }
        }
    };
}
